/**
 * @Description: test
 * @Author: Hu Outstanding
 * @Date: 2022/2/7 1:35
 */
package com.aaa.three.service.impl;

import com.aaa.three.mapper.AdminInfoMapper;
import com.aaa.three.pojo.domain.*;
import com.aaa.three.pojo.query.AdminInfoQuery;
import com.aaa.three.pojo.vo.AdminVO;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.AdminInfoService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminInfoServiceImpl implements AdminInfoService {
    @Autowired
    private AdminInfoMapper adminInfoMapper;


    @Override
    public AjaxResult queryAdminInfo(AdminInfoQuery query) {

        List<AdminInfo> adminInfos = adminInfoMapper.queryAdminInfo(query);
        Long aLong = adminInfoMapper.queryAdminInfoTotal(query);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("currentData",adminInfos);
        map.put("totalCount",aLong);

        return AjaxResult.success(map);
    }
    @Override
    public AjaxResult deleteAdmin(int id) {
        int i = adminInfoMapper.deleteAdmin(id);
        if (i > 0 ){
            return AjaxResult.success();
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult queryDname() {
        List<AdminDept> queryDname= adminInfoMapper.queryDname();
        return AjaxResult.success(queryDname);
    }

    @Override
    public AjaxResult queryOname(int id) {
        List<AdminOffice> queryOname = adminInfoMapper.queryOname(id);
        return AjaxResult.success(queryOname);
    }

    @Override
    public AjaxResult queryRname(int id) {
        List<AdminRole> queryRname = adminInfoMapper.queryRname(id);
        return AjaxResult.success(queryRname);
    }

    @Override
    public AjaxResult addAdmin(AdminVO adminVO) {

        String newPhone = adminVO.getAphone();
        int q = adminInfoMapper.queryCount(newPhone);
        if (q == 0){
            Integer did = adminVO.getDid();
            int ds = adminInfoMapper.getDeptState(did);
            if (ds == 1){
                Integer oid = adminVO.getOid();
                int os = adminInfoMapper.getOfficeState(oid);
                if (os == 1){
                    int i = adminInfoMapper.addAdmin(adminVO);
                    if (i > 0 ){
                        int newId =  adminVO.getId();
                        int a = adminInfoMapper.addLogin(newPhone,newId);
                        if (a>0){
                            return AjaxResult.success();
                        }
                    }
                    return AjaxResult.fail("添加失败");
                }
                return AjaxResult.fail("科室未启用，请联系科长");
            }
            return AjaxResult.fail("部门未启用，请联系部长");
        }
        return AjaxResult.fail("该手机号已经注册");
    }

    @Override
    public Integer addFiles(AdminVO object) {


        String newPhone = object.getAphone();

        int q = adminInfoMapper.queryCount(newPhone);
        if (q == 0){
            int i = adminInfoMapper.addFiles(object);
            if (i > 0 ){
                int newId =  object.getId();
                int a = adminInfoMapper.addLogin(newPhone,newId);
                if (a>0){
                    return i;
                }
            }
        }
        return 0;

    }

    @Override
    public AjaxResult getAdminInfo(int id) {
        AdminVO adminVO = adminInfoMapper.getyAdminInfo(id);
        return AjaxResult.success(adminVO);

    }

    @Override
    public AjaxResult getOname() {
        List<AdminOffice> getOname = adminInfoMapper.getOname();

        return AjaxResult.success(getOname);
    }

    @Override
    public AjaxResult getDname() {
        List<AdminDept> getDname = adminInfoMapper.getDname();

        return AjaxResult.success(getDname);
    }

    @Override
    public AjaxResult getRname() {
        List<AdminRole> getRname = adminInfoMapper.getRname();

        return AjaxResult.success(getRname);
    }

    @Override
    public AjaxResult updateAdmin(AdminVO adminVO) {
        int i = adminInfoMapper.updateAdmin(adminVO);
        if (i > 0 ){
            return AjaxResult.success();
        }
        return AjaxResult.fail("修改失败");
    }

    @Override
    public AjaxResult queryEducation() {
        List<AdminEducation> queryEducation = adminInfoMapper.queryEducation();
        return AjaxResult.success(queryEducation);
    }

}
